package com.csw.hadoopMr;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class FlowCount {

	static class FlowCountMapper extends Mapper<LongWritable, Text, Text, FlowBean> {
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			// 将一行内容转换成string
			String line = value.toString();
			// 切分字段
			String[] fields = line.split("\t");
			// 取出手机号
			String phoneNumber = fields[1];
			// 取出上行流量和下行流量
			long upFlow = Long.parseLong(fields[fields.length - 3]);
			long downFlow = Long.parseLong(fields[fields.length - 2]);
			context.write(new Text(phoneNumber), new FlowBean(upFlow, downFlow));
		}
	}

	static class FlowCountReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
		@Override
		protected void reduce(Text key, Iterable<FlowBean> values, Context context)
				throws IOException, InterruptedException {
			long sumUpFlow = 0;
			long sumDownFlow = 0;
			// 遍历所有的bean,将其中的上行流量,下行流量分别累加
			for (FlowBean bean : values) {
				sumUpFlow += bean.getUpFlow();
				sumDownFlow += bean.getDownFlow();
			}
			FlowBean resultBean = new FlowBean(sumUpFlow, sumDownFlow);
			context.write(key, resultBean);
		}
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		job.setJarByClass(FlowCount.class);
		job.setMapperClass(FlowCountMapper.class);
		job.setReducerClass(FlowCountReducer.class);

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(FlowBean.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(FlowBean.class);

		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		boolean res = job.waitForCompletion(true);
		System.exit(res ? 0 : 1);

	}

}
/*
 * 
 * ———————————————— 版权声明：本文为CSDN博主「汤愈韬」的原创文章，遵循 CC 4.0 BY-SA
 * 版权协议，转载请附上原文出处链接及本声明。
 * 原文链接：https://blog.csdn.net/qq_38200548/article/details/83627098
 */
